package com.shiguiwu.springboot3.feign;

import cn.hutool.core.util.TypeUtil;
import com.shiguiwu.springboot3.properties.OpenfeignProperties;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @description: 结果工厂
 * @author: stone
 * @date: Created by 2023/9/23 9:21
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.feign
 */
@Component
public class OpenfeignResultFactory {

    @Autowired
    private OpenfeignProperties openfeignProperties;

    List<OpenfeignProperties.ResultConfig> results = new CopyOnWriteArrayList<>();


    @PostConstruct
    public void init() {
        //设置内置的

        //设置外部的
        results.addAll(openfeignProperties.getResult());
    }


    public OpenfeignProperties.ResultConfig parseConfig(Type type) {
        Class<?> aClass = TypeUtil.getClass(type);
        return results.stream()
                .filter(r -> aClass.getName().contains(r.getResultClass()))
                .findFirst()
                .orElseThrow(() -> new RuntimeException("找不到任务类！！！"));
    }


}
